import React from 'react'
// ===================================================================== Temp
import Table from '#cpt/temp/Table'
import AddEditModal from '#cpt/temp/AddEditModal'
import TableControls from '#cpt/temp/TableControls'
import Controls from '#cpt/temp/Controls'
import Enable from '#cpt/temp/Enable'
// ===================================================================== global declare
const { $fn, $Lazy } = window
// ===================================================================== Lazy
const Page 					= $Lazy.load(()=>import('#tp/page/page-container'))
// ===================================================================== component
class Index extends React.Component{
	state = {
		data:[],
		pag: {},
		selectedKeys:[],
		// 提交表单列表
				formData : [
					{ label: '集团名称',		name: 'group_name', 		full:true, required:true  },
					{ label: 'Logo',		name: 'logo', 				full:true, required:true, type:'upload', code:118 },
					{ label: '中文简称',		name: 'short_name', 		half:true },
					{ label: '英文名称',		name: 'en_name', 			half:true },
					{ label: '英文简称',		name: 'short_en_name', 		half:true },
					{ label: '集团编号',		name: 'corp_group_code', 	half:true, required:true },
					{ label: '区域',		name: 'areaData', 			half:true, required:true, type: 'address', onRef: ref=> this.address = ref },
					{ label: '地址',		name: 'address', 			half:true, required:true },
					{ label: '英文地址',		name: 'en_address', 		half:true },
					{ label: '法人代表',		name: 'legal_person', 		half:true, required:true },
					{ label: '固定电话',		name: 'tel', 				half:true, required:true },
					{ label: '注册日期',		name: 'reg_date', 			half:true, required:true, type:'date-time' },
					{ label: '排序',		name: 'sort', 				half:true, type:'number' },
					{ label: '集团网址',		name: 'website', 			half:true, required:true },
					{ label: '集团简介',		name: 'summary', 			full:true, type:'textarea' },
					{ label: '集团描述',		name: 'description', 		full:true, type:'textarea' },
				]
	}
	
	model = {}
	
	componentDidMount(){
		this.fetch()
	}
	// paging
	fetch = () => $fn.fetch.call(this,'corp-group/index')
	
	// 表单
		cols = [
			{ title: '集团编码',		dataIndex: 'corp_group_code', 	width:100 },
			{ title: '中文名称', 	dataIndex: 'group_name', 		width:150 },
			{ title: '简称', 		dataIndex: 'short_name', 		width:150 },
			{ title: '英文名称', 	dataIndex: 'en_name', 			width:150 },
			{ title: '英文简称', 	dataIndex: 'short_en_name', 	width:150, align:'center' },
			{ title: '状态', 		dataIndex: 'enabled', 			...$fn.table.status,  render: text => +text === 1 ? '启用' : '禁用' },
			TableControls.controls.call(this,{ width: 150 })
		]
	// 表格操作按钮
	table_controls = (text,{ uuid, enabled }) => {
		return [
			// 编辑
			TableControls.edit.call(this, 'corp-group/info', { uuid }, {
				address: value => this.address.load(value)
			}),							
			+enabled === -1 && this.onEnabled(1, [ uuid ]),										// 启用
			+enabled === 1 && this.onEnabled(0, uuid),											// 禁用
			TableControls.pushTo.call(this, '查看', `company-manage?uuid=${uuid}`),				// 查看
		]
	}
	// 操作按钮
	controls = () => {
		return [
			Controls.add.call(this),			// 添加
			this.onEnabled(1),					// 启用
		]
	}
	// 禁用/启用
	onEnabled = (enabled, uuid) => Enable.call(this, enabled,  { uuid, on:'corp-group/open', off:'corp-group/del', isKeys: !uuid, onComplete:({ param })=>{$fn.dataClear()}, })
	
	render(){
		return (
			<>
				<Page title='集团管理' controls={this.controls()}>
					{/* 表格 */}
					{ Table.call(this) }
				</Page>
				{/* 添加、编辑 */}
				{ AddEditModal.call(this,{ api: 'corp-group/add', editApi:'corp-group/edit', onComplete:({ param })=>{$fn.dataClear()}, }) }
			</>
		)
	}
}

export default Index